package routers

import (
	"TheTearAfter/controller"

	"github.com/gin-gonic/gin"
)

/**
 * @File : routers.go
 * @Description : 设置路由
 * @Author : chen
 * @Date : 2024/4/8
 */

// CORS 中间件
func CORS() gin.HandlerFunc {
	return func(c *gin.Context) {
		c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
		c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
		c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type")

		if c.Request.Method == "OPTIONS" {
			c.AbortWithStatus(204)
			return
		}
		c.Next()
	}
}

// 设置路由
func SetupRouters() (r *gin.Engine) {
	r = gin.Default()
	r.Use(CORS())

	// message 相关路由
	messageGroup := r.Group("/message")
	{
		// 获取类列表
		messageGroup.POST("/getMessageTypeClassList", controller.GetMessageTypClassList)
		// 获取每个类信息块列表
		messageGroup.POST("/getClassMessageBlockList", controller.GetClassMessageBlockList)
	}

	return
}
